﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Drawing.Printing;
using System.IO;
using System.Drawing;
using System.Printing;


namespace WebApplication1
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public List<FoodSell> GetFood()
        {
            FoodSell food1 = new FoodSell();
            food1.name = "Nasi Lemak";
            food1.price = "RM8";
            food1.description = "This is testing ...";

            FoodSell food2 = new FoodSell();
            food2.name = "Nasi Goring";
            food2.price = "RM7";
            food2.description = "This is testing ...";

            List<FoodSell> foods = new List<FoodSell>();
            foods.Add(food1);
            foods.Add(food2);
            return foods;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //List<string> email = new List<string>();
            //email.Add("sekhor@gmail.com");
            //email.Add("chinteongtan@hotmail.com");
            //Common.SendMail("Testing", email, "Added email class, sending thru gmail account testing");
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
        }

        private StreamReader streamToPrint;
        private Font printFont;
        protected void TestPrintBtn_Click(object sender, EventArgs e)
        {
            // Enum printer
            String pkInstalledPrinters;
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
                System.Diagnostics.Debug.WriteLine("Installed Printer:" + pkInstalledPrinters);
                LocalPrintServer printServer = new LocalPrintServer();
                PrintQueue pq = printServer.GetPrintQueue(pkInstalledPrinters);

                string jobList = "";
                string statusReport = "";
                pq.Refresh();
                PrintJobInfoCollection jobs = pq.GetPrintJobInfoCollection();
                foreach (PrintSystemJobInfo job in jobs)
                {
                    // Since the user may not be able to articulate which job is problematic, 
                    // present information about each job the user has submitted. 
                    if (true)
                    {
                        
                        jobList = "\tQueue:" + pq.Name;
                        jobList = jobList + "\n\tLocation:" + pq.Location;
                        jobList = jobList + "\n\t\tJob: " + job.JobName + " ,ID: " + job.JobIdentifier + " Submitted:"+ job.Submitter;
                        jobList = jobList + "\n\t\tJob Status: " + job.JobStatus + ", Submitted Time:" + job.TimeJobSubmitted;
                        jobList = jobList + "\n\t\tPrinter Description: " + pq.CurrentJobSettings.Description;
                        jobList = jobList + "\n\t\tJob Name: " + job.Name;
                        SpotTroubleUsingProperties(ref statusReport, pq);
                        jobList = jobList + "\n\tStatus Report: " + statusReport;
                        System.Diagnostics.Debug.WriteLine(jobList);
                    }
                }// end for each print job  
            }


            try
            {

                streamToPrint = new StreamReader("C:\\Temp\\PrintPage.txt");
                //string printer = "Kyocera Mita KM-2550 KX";
                string printer = "EASYSET PBP_A7";
                try
                {
                    printFont = new Font("Arial", 10);

                    PrintDocument pd = new PrintDocument();
                    pd.PrintPage += new PrintPageEventHandler
                       (this.pd_PrintPage);

                    pd.PrinterSettings.PrinterName = printer;
                    pd.DocumentName = "FlexFood Ordering";
                    
                    pd.Print();
                }
                finally
                {
                    streamToPrint.Close();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left-90;
            float topMargin = ev.MarginBounds.Top-90;
            string line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height /
               printFont.GetHeight(ev.Graphics);
            System.Diagnostics.Debug.WriteLine("linesPerPage=" + linesPerPage.ToString());
            System.Diagnostics.Debug.WriteLine("ev.MarginBounds.Height=" + ev.MarginBounds.Height.ToString());
            System.Diagnostics.Debug.WriteLine("ev.MarginBounds.Width=" + ev.MarginBounds.Width.ToString());

            // Print each line of the file. 
            while (count < linesPerPage &&
               ((line = streamToPrint.ReadLine()) != null))
            {
                printFont = new Font("Arial", 10-count);

                line = "Microsoft Test";
                yPos = topMargin + (count *
                   printFont.GetHeight(ev.Graphics));

                ev.Graphics.DrawString(line, printFont, Brushes.Black,
                   leftMargin, yPos, new StringFormat());

                count++;
            }
            string line2 = ("ev.MarginBounds.Width=" + ev.MarginBounds.Width.ToString());
            printFont = new Font("Verdana", 10);
            yPos = topMargin + (5*printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line2, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());

            line2 = ("ev.MarginBounds.Width=" + ev.MarginBounds.Height.ToString());
            printFont = new Font("Verdana", 10);
            yPos = topMargin + (6 * printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line2, printFont, Brushes.Black, leftMargin+20, yPos, new StringFormat());

            // If more lines exist, print another page. 
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }

        internal static void SpotTroubleUsingProperties(ref String statusReport, PrintQueue pq)
        {
            if (pq.HasPaperProblem)
            {
                statusReport = statusReport + "Has a paper problem. ";
            }
            if (!(pq.HasToner))
            {
                statusReport = statusReport + "Is out of toner. ";
            }
            if (pq.IsDoorOpened)
            {
                statusReport = statusReport + "Has an open door. ";
            }
            if (pq.IsInError)
            {
                statusReport = statusReport + "Is in an error state. ";
            }
            if (pq.IsNotAvailable)
            {
                statusReport = statusReport + "Is not available. ";
            }
            if (pq.IsOffline)
            {
                statusReport = statusReport + "Is off line. ";
            }
            if (pq.IsOutOfMemory)
            {
                statusReport = statusReport + "Is out of memory. ";
            }
            if (pq.IsOutOfPaper)
            {
                statusReport = statusReport + "Is out of paper. ";
            }
            if (pq.IsOutputBinFull)
            {
                statusReport = statusReport + "Has a full output bin. ";
            }
            if (pq.IsPaperJammed)
            {
                statusReport = statusReport + "Has a paper jam. ";
            }
            if (pq.IsPaused)
            {
                statusReport = statusReport + "Is paused. ";
            }
            if (pq.IsTonerLow)
            {
                statusReport = statusReport + "Is low on toner. ";
            }
            if (pq.NeedUserIntervention)
            {
                statusReport = statusReport + "Needs user intervention. ";
            }

            // Check if queue is even available at this time of day 
            // The following method is defined in the complete example.
            //ReportAvailabilityAtThisTime(ref statusReport, pq);

        }//end SpotTroubleUsingProperties



        protected void Button2_Click(object sender, EventArgs e)
        {

            Order myorder = new Order();
            fos_Order ordertemp = new fos_Order();
            myorder.SeparateOrderByCatererNPrint(ordertemp);
        }
    }

    public class FoodSell
    {
        public string name { get; set; }
        public string price { get; set; }
        public string description { get; set; }
    }

    /* may consist of different cafe ==> different printer
select fo.id OrderID, fo.CreatedDate OrderTime, u.email as OrderBy, fo.PickUpdateTime, fo.Remark OrderRemark
,fod.FoodQuantity, fod.FoodPrice
,fod.foodname
,fd.CreatedBy as Caterer
,pr.printername
from fos_order fo
left join fos_orderdetails fod on fo.id = fod.orderid
left join fos_user u on fo.OrderBy = u.id
left join fos_food fd on fd.id = fod.foodid
left join fos_user u2 on u2.email = fd.CreatedBy
left join fos_SiteLinkPrinter slp on slp.userid = u2.id
left join fos_printer pr on pr.id= slp.printerid
where (slp.isDeleted != 1 OR slp.isdeleted is null) and fo.id=38
order by fo.id,fd.CreatedBy ;
*/
    /*
     * from fo in Fos_Orders
join fod in Fos_orderdetails on fo.Id equals fod.OrderID
join u in Fos_Users on fo.OrderBy equals u.Id
join fd in Fos_Foods on  fod.FoodId equals fd.Id
join u2 in Fos_Users on  fd.CreatedBy equals u2.Email
join slp in Fos_SiteLinkPrinters on u2.Id equals slp.UserId 
join pr in Fos_Printers on  slp.PrinterId equals pr.Id 
where (slp.IsDeleted != 1 || slp.IsDeleted==null) && fo.Id==38
select new
{OrderID = fo.Id ,OrderTime = fo.CreatedDate ,OrderBy= u.Email
,PickUpdateTime= fo.PickUpDateTime,OrderRemark= fo.Remark 
,FoodQuantity = fod.FoodQuantity, FoodPrice = fod.FoodPrice
,foodname = fod.FoodName
,Caterer = fd.CreatedBy 
,printername = pr.PrinterName
}
     */
}